package com.ruoyi.labdata.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.labdata.domain.LabTwoDimensionalCode;
import com.ruoyi.labdata.service.ILabTwoDimensionalCodeService;

/**
 * 二维码Controller
 *
 * @author lzlong
 * @date 2022-03-07
 */
@RestController
@RequestMapping("/labdata/twoDimensionalCode")
public class LabTwoDimensionalCodeController extends BaseController
{
    @Autowired
    private ILabTwoDimensionalCodeService labTwoDimensionalCodeService;

    /**
     * 查询二维码列表
     */
    @PreAuthorize("@ss.hasPermi('labdata:twoDimensionalCode:query')")
    @GetMapping("/list")
    public TableDataInfo list(LabTwoDimensionalCode labTwoDimensionalCode)
    {
        startPage();
        List<LabTwoDimensionalCode> list = labTwoDimensionalCodeService.selectLabTwoDimensionalCodeList(labTwoDimensionalCode);
        return getDataTable(list);
    }

    /**
     * 导出二维码列表
     */
   /* @PreAuthorize("@ss.hasPermi('labdata:twoDimensionalCode:export')")
    @Log(title = "二维码", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabTwoDimensionalCode labTwoDimensionalCode,HttpServletResponse response) throws Exception {
        List<LabTwoDimensionalCode> list = labTwoDimensionalCodeService.selectLabTwoDimensionalCodeList(labTwoDimensionalCode);
        // 设置二维码已经导出
        labTwoDimensionalCodeService.updateLabTwoDimensionalCodeExprot(list);
        ExcelUtil<LabTwoDimensionalCode> util = new ExcelUtil<LabTwoDimensionalCode>(LabTwoDimensionalCode.class);
        return util.exportExcel(list, "twoDimensionalCode");
    }*/

    /**
     * 获取二维码详细信息
     */
    @PreAuthorize("@ss.hasPermi('labdata:twoDimensionalCode:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(labTwoDimensionalCodeService.selectLabTwoDimensionalCodeById(id));
    }

    /**
     * 新增二维码
     */
    @PreAuthorize("@ss.hasPermi('labdata:twoDimensionalCode:edit')")
    @Log(title = "二维码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabTwoDimensionalCode labTwoDimensionalCode)
    {
        return toAjax(labTwoDimensionalCodeService.insertLabTwoDimensionalCode(labTwoDimensionalCode));
    }

    /**
     * 修改二维码
     */
    @PreAuthorize("@ss.hasPermi('labdata:twoDimensionalCode:edit')")
    @Log(title = "二维码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabTwoDimensionalCode labTwoDimensionalCode)
    {
        return toAjax(labTwoDimensionalCodeService.updateLabTwoDimensionalCode(labTwoDimensionalCode));
    }

    /**
     * 删除二维码
     */
    @PreAuthorize("@ss.hasPermi('labdata:twoDimensionalCode:edit')")
    @Log(title = "二维码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(labTwoDimensionalCodeService.deleteLabTwoDimensionalCodeByIds(ids));
    }

    @GetMapping(value = "/imageDowload/image")
    public String downloadImage(Long id, HttpServletRequest request, HttpServletResponse response) {
        //String fileName = "123.JPG";
        LabTwoDimensionalCode labTwoDimensionalCode = labTwoDimensionalCodeService.selectLabTwoDimensionalCodeById(id);
        if(labTwoDimensionalCode==null){
            throw new RuntimeException("未能查找到该ID对应的图片:" + id);
        }
        String fileUrl = RuoYiConfig.getProfile() + StringUtils.substringAfter(labTwoDimensionalCode.getFilePath(), Constants.RESOURCE_PREFIX);
        File file = new File(fileUrl);
        if (file.exists()) {
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.setHeader("Content-Type","video/mpeg4");
            response.addHeader("Content-Disposition",
                    "attachment;fileName=" + labTwoDimensionalCode.getCode()+".png");// 设置文件名
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                System.out.println("success");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }

}
